package com.zhentao.controller;

import com.zhentao.dtos.Result;
import com.zhentao.model.entity.product.Product;
import com.zhentao.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("findAll")
    public Result findAll() {
        List< Product > list = null;
        try {
            list = productService.findAll();
            return Result.success( list );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    @RequestMapping("findById")
    public Result findById( Long id ) {
        Product product = null;
        try {
            product = productService.findById( id );
            return Result.success( product );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    @RequestMapping("selectByName")
    public Result selectByName( String name ) {
        List< Product > list = null;
        try {
            list = productService.selectByName( name );
            return Result.success( list );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    @RequestMapping("selectByParentCategory")
    public Result selectByParentCategory( Integer id ) {
        List< Product > list = null;
        try {
            list = productService.selectByParentCategory( id );
            return Result.success( list );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    @RequestMapping("selectBySonCategory")
    public Result selectBySonCategory( Integer id ) {
        List< Product > list = null;
        try {
            list = productService.selectBySonCategory( id );
            return Result.success( list );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    @RequestMapping("selectBySonCategory2")
    public Result selectBySonCategory2( Integer id ) {
        List< Product > list = null;
        try {
            list = productService.selectBySonCategory2( id );
            return Result.success( list );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }
}
